using System;
using System.Collections.Generic;
using System.Reflection;
using CardGameObject.GameScene;
using Entity;
using Mapper;
using Newtonsoft.Json;

namespace Utils
{
    public static class LaunchEffectUtil
    {
        /// <summary>
        /// 根据传入的实现类名、JSON 格式的参数和方法名来调用对应的方法
        /// </summary>
        /// <param name="effectClass">实现类的类名</param>
        /// <param name="effectParams">调用方法时需要的参数，JSON 格式的字符串，原本类型为 object 数组</param>
        /// <param name="effectMethod">实现类中的方法名，即需要调用的方法名</param>
        public static void LaunchEffect(string effectClass, string effectParams, string effectMethod)
        {
            // 加载当前执行程序集
            Assembly assembly = Assembly.GetExecutingAssembly();
            // 获取指定类的类型
            Type type = assembly.GetType(effectClass);
            if (type == null)
            {
                return;
            }

            // 创建类的实例
            object instance = Activator.CreateInstance(type);

            // 解析 JSON 格式的参数
            object[] parameters = JsonConvert.DeserializeObject<object[]>(effectParams);
            if (parameters == null)
            {
                parameters = new object[0];
            }

            // 获取要调用的方法
            MethodInfo method = type.GetMethod(effectMethod);
            if (method == null)
            {
                return;
            }

            // 获取方法参数信息
            ParameterInfo[] methodParameters = method.GetParameters();

            // 检查参数数量是否匹配
            if (parameters.Length != methodParameters.Length)
            {
                return;
            }

            // 转换参数类型
            for (int i = 0; i < parameters.Length; i++)
            {
                Type targetType = methodParameters[i].ParameterType;
                object param = parameters[i];

                try
                {
                    if (param is long && targetType == typeof(int))
                    {
                        // 处理 long 转 int
                        parameters[i] = (int)(long)param;
                    }
                    else if (param is string && targetType == typeof(string))
                    {
                        // 保持 string 类型不变
                        continue;
                    }
                    else if (param != null)
                    {
                        // 尝试进行通用类型转换
                        parameters[i] = Convert.ChangeType(param, targetType);
                    }
                }
                catch (Exception)
                {
                    // 类型转换失败，退出方法
                    return;
                }
            }

            // 调用方法
            method.Invoke(instance, parameters);
        }


        public static void LaunchSpellEffect(GameCard card)
        {
            EffectMapper effectMapper = new EffectMapper();
            List<Effect> effectList = effectMapper.GetEffectByCardId(card.CardId);
            foreach (Effect effect in effectList)
            {
                LaunchEffect(effect.EffectClass, effect.EffectParams, effect.EffectMethod);
            }
            effectMapper.CloseConnection();
        }

        public static void LaunchMonsterEffect(GameCard card , string launchTime)
        {
            EffectMapper effectMapper = new EffectMapper();
            List<Effect> effectList = effectMapper.GetEffectByLaunchTimeAndCardId(launchTime, card.CardId);
            foreach (Effect effect in effectList)
            {
                LaunchEffect(effect.EffectClass, effect.EffectParams, effect.EffectMethod);
            }
            effectMapper.CloseConnection();
        }
    }
}